<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@ page import="java.util.List"%>
<%@ page import="java.text.SimpleDateFormat"%>
<%@ page import="java.util.Date"%>
<%@ page import="java.util.TimeZone"%>
<%@ page import="com.google.appengine.api.users.User"%>
<%@ page import="com.google.appengine.api.users.UserService"%>
<%@ page import="com.google.appengine.api.users.UserServiceFactory"%>
<%@ page import="com.google.appengine.api.datastore.DatastoreServiceFactory"%>
<%@ page import="com.google.appengine.api.datastore.DatastoreService"%>
<%@ page import="com.google.appengine.api.datastore.Query"%>
<%@ page import="com.google.appengine.api.datastore.Entity"%>
<%@ page import="com.google.appengine.api.datastore.FetchOptions"%>
<%@ page import="com.google.appengine.api.datastore.Key"%>
<%@ page import="com.google.appengine.api.datastore.KeyFactory"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>

<html>
<head>
<title>LLC UH Manoa:: Admin view user of user data store</title>
<link type="text/css" rel="stylesheet" href="/stylesheets/main.css" />
</head>

<body>
  <h2>Users and Scores</h2>
  <table class="table1">
    <tr>
      <th>Email</th>
      <th>Quiz Name</th>
      <th>Raw Score</th>
      <th>Score</th>
      <th>DateTime</th>
    </tr>
    <%
		pageContext.setAttribute("email", pageContext.getAttribute("email"));
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a"); 
        Date date;
        String sDate;

        // Get the data
        DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();

        Query query = new Query("User");
        List<Entity> users = datastore.prepare(query).asList(FetchOptions.Builder.withLimit(20));

        for (Entity user : users)
        {
            Key userKey = user.getKey();

            System.err.println(user + "\n");

            pageContext.setAttribute("email", user.getProperty("email"));
            pageContext.setAttribute("quizname", user.getProperty("quizname"));
            pageContext.setAttribute("rawscore", user.getProperty("rawscore"));
            pageContext.setAttribute("score", user.getProperty("score"));
            date = (Date) user.getProperty("dateTime");
            sdf.setTimeZone(TimeZone.getTimeZone("HST"));
            sDate= sdf.format(date);
            pageContext.setAttribute("dateTime", sDate);
    %>
    <tr>
      <td>${fn:escapeXml(email)}</td>

      <td>${fn:escapeXml(quizname)}</td>

      <td>${fn:escapeXml(rawscore)}</td>

      <td>${fn:escapeXml(score)}</td>

      <td>${fn:escapeXml(dateTime)}</td>

    </tr>

    <%
        } 
		
    %>

  </table>


</body>
</html>